package cn.hls.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import cn.hls.dao.CompanyDao;
import cn.hls.dao.GoodsCategoryDao;
import cn.hls.dao.GoodsDao;
import cn.hls.dao.ShipOrderDao;
import cn.hls.dao.ShipOrderGoodsDao;
import cn.hls.dao.ShipOrderSiteDao;
import cn.hls.dao.ShipperDao;
import cn.hls.dao.SiteDao;
import cn.hls.entity.Company;
import cn.hls.entity.Goods;
import cn.hls.entity.GoodsCategory;
import cn.hls.entity.ShipOrder;
import cn.hls.entity.ShipOrderGoods;
import cn.hls.entity.ShipOrderSite;
import cn.hls.entity.Shipper;
import cn.hls.entity.Site;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.ShipOrderService;
import cn.hlsconstant.CommonEnum;
/**
  *@fileName: ShipOrderServiceBean.java
  *@package: cn.hls.service.impl
　*@Description:托运方基本信息业务逻辑接口实现类
　*@author: yinghui.zhang
　*@email: uuhui@163.com
  *@date: 2016年6月10日
 *
 */
@Component("shipOrderService")
public class ShipOrderServiceBean extends BaseServiceBean<ShipOrder> implements
		ShipOrderService {
	@Resource(name = "shipOrderDao")
	private ShipOrderDao dao;
	@Resource(name="shipOrderGoodsDao")
	private ShipOrderGoodsDao shipOrderGoodsDao;
	@Resource(name="shipOrderSiteDao")
	private ShipOrderSiteDao shipOrderSiteDao;
	@Resource(name="companyDao")
	private CompanyDao companyDao;//企业数据操作类对象
	@Resource(name="shipperDao")
	private ShipperDao shipperDao;//托运方数据操作对象
	@Resource(name="siteDao")
	private SiteDao siteDao;//站点数据操作对象
	@Resource(name="goodsDao")
	private GoodsDao goodsDao;//货物数据操作对象
	@Resource(name="goodsCategoryDao")
	private GoodsCategoryDao goodsCategoryDao;//货物类型数据操作对象
	@Override
	public boolean add(ShipOrder entity) throws Exception {
		boolean flag=true; 
		Shipper shipperEntity=entity.getShipper();
		if(shipperEntity.getId()==null){//编号不存在时，实例托运方对象托
			Company companyEntity=shipperEntity.getCompany();
			if(companyEntity.getId()==null){//编号不存在时，实例企业对象
				companyEntity.setId(companyDao.getCustomerId());
				Site site=entity.getShipOrderSendSites().get(0).getSite();
				companyEntity.setProvinceId(site.getProvinceId());
				companyEntity.setProvinceName(site.getProvinceName());
				companyEntity.setCityId(site.getCityId());
				companyEntity.setCityName(site.getCityName());
				companyEntity.setAreaId(site.getAreaId());
				companyEntity.setAreaName(site.getAreaName());
				flag&=companyDao.add(companyEntity);
			}
			shipperEntity.setId(shipperDao.getCustomerId());
			flag&=shipperDao.add(shipperEntity);
		}
		flag&=super.add(entity);
		//新增收货点、发货点地址信息
		if(flag){
			//添加发货站点中间点信息
			for(ShipOrderSite item:entity.getShipOrderSendSites()){
				Site siteEntity=item.getSite();
				if(siteEntity.getId()==null){//编号不存在时，实例站点对象
					siteEntity.setType(2);//设置站点的类型为 发货地址
					siteEntity.setId(siteDao.getCustomerId());
					flag&=siteDao.add(siteEntity);
				}
				item.setId(shipOrderSiteDao.getCustomerId());
				item.setShipOrderId(entity.getId());
				flag&=shipOrderSiteDao.add(item);
			}
			//添加收货站中间点信息
			for(ShipOrderSite item:entity.getShipOrderReceiveSites()){
				Site siteEntity=item.getSite();
				if(siteEntity.getId()==null){//编号不存在时，实例站点对象
					siteEntity.setType(1);//设置站点的类型为 收货地址
					siteEntity.setId(siteDao.getCustomerId());
					flag&=siteDao.add(siteEntity);
				}
				item.setId(shipOrderSiteDao.getCustomerId());
				item.setShipOrderId(entity.getId());
				flag&=shipOrderSiteDao.add(item);
			}
		}
		if(flag){
			//新增货物中间表信息
			for(ShipOrderGoods item:entity.getShipOrderGoods()){
				Goods goodsEntity=item.getGoods();
				if(goodsEntity.getId()==null){//编号不存在时，实例化货物对象
					GoodsCategory goodsCategoryEntity=goodsEntity.getCategory();
					if(goodsCategoryEntity.getId()==null){//编号不存在时，实例化货物类型对象
						goodsCategoryEntity.setId(goodsCategoryDao.getCustomerId());
						flag&=goodsCategoryDao.add(goodsCategoryEntity);
					}
					goodsEntity.setId(goodsDao.getCustomerId());
					flag&=goodsDao.add(goodsEntity);
				}
				item.setId(shipOrderGoodsDao.getCustomerId());
				item.setShipOrderId(entity.getId());
				flag&=shipOrderGoodsDao.add(item);
			}
		}
		return flag;
	}
	@Override
	public ShipOrder find(Serializable entityId) throws Exception {
		ShipOrder entity= dao.find(entityId);
		if(entity!=null){
			List<ShipOrderGoods> ShipOrderGoodsList=shipOrderGoodsDao.loadShipOrderGoodsList(new ShipOrderGoods((Long)entityId));
			if (ShipOrderGoodsList!=null && !ShipOrderGoodsList.isEmpty()){
				entity.setShipOrderGoods(ShipOrderGoodsList);
			}
			//发货站点信息
			List<ShipOrderSite> shipOrderSendList=shipOrderSiteDao.loadShipOrderSiteList(new ShipOrderSite((Long)entityId, Integer.parseInt(CommonEnum.SENDSITE.getCode())));
			if(shipOrderSendList!=null && !shipOrderSendList.isEmpty()){
				entity.setShipOrderSendSites(shipOrderSendList);
			}
			//收货地址信息
			List<ShipOrderSite>shipOrderReceiveList=shipOrderSiteDao.loadShipOrderSiteList(new ShipOrderSite((Long)entityId, Integer.parseInt(CommonEnum.RECEIVESITE.getCode())));
			if(shipOrderReceiveList!=null && !shipOrderReceiveList.isEmpty()){
				entity.setShipOrderReceiveSites(shipOrderReceiveList);
			}
		}
		return entity;
	}
	@Override
	public PageInfo<ShipOrder> loadShipOrderInfo(ShipOrder entity,
			PageInfo<ShipOrder> page) throws Exception {
		return dao.loadShipOrderInfo(entity, page);
	}
	
	@Override
	public List<ShipOrder> loadShipOrderList(ShipOrder entity)throws Exception{
		return dao.loadShipOrderList(entity);
	}
	
	public ShipOrder getShipOrderBycar(String licenseNum,String licenseNum2)throws Exception{
		ShipOrder entity= dao.getShipOrderBycar(licenseNum,licenseNum2);
		if(entity!=null){
			List<ShipOrderGoods> ShipOrderGoodsList=shipOrderGoodsDao.loadShipOrderGoodsList(new ShipOrderGoods(entity.getId()));
			if (ShipOrderGoodsList!=null && !ShipOrderGoodsList.isEmpty()){
				entity.setShipOrderGoods(ShipOrderGoodsList);
			}
			//发货站点信息
			List<ShipOrderSite> shipOrderSendList=shipOrderSiteDao.loadShipOrderSiteList(new ShipOrderSite(entity.getId(), Integer.parseInt(CommonEnum.SENDSITE.getCode())));
			if(shipOrderSendList!=null && !shipOrderSendList.isEmpty()){
				entity.setShipOrderSendSites(shipOrderSendList);
			}
			//收货地址信息
			List<ShipOrderSite>shipOrderReceiveList=shipOrderSiteDao.loadShipOrderSiteList(new ShipOrderSite(entity.getId(), Integer.parseInt(CommonEnum.RECEIVESITE.getCode())));
			if(shipOrderReceiveList!=null && !shipOrderReceiveList.isEmpty()){
				entity.setShipOrderReceiveSites(shipOrderReceiveList);
			}
		}
		return entity;
	}

	@Override
	public boolean exists(ShipOrder entity) throws Exception {
		List<Long>shipOrderIdList=dao.loadShipOrderIdList(entity);
		if(shipOrderIdList==null || shipOrderIdList.size()==0){
			return false;//不存在数据
		}else{
			return shipOrderGoodsDao.exists(shipOrderIdList, entity.getShipOrderGoods());
		}
	}
	
	@Override
	public void delete(Object entityId) throws Exception {
		dao.delete(entityId);
	}
	
	
	@Override
	public boolean update(ShipOrder entity) throws Exception {
		ShipOrder tempShipOrder=dao.find(entity.getId());
		List<ShipOrderGoods>list1=entity.getShipOrderGoods();
		List<ShipOrderGoods>list2=shipOrderGoodsDao.loadShipOrderGoodsList(new ShipOrderGoods(entity.getId()));
		Date createTime=tempShipOrder.getCreateTime();
		BeanUtils.copyProperties(entity, tempShipOrder);
		tempShipOrder.setCreateTime(createTime);
		tempShipOrder.setUpdateTime(new Date());
		boolean flag=true;
		if((list2==null) ||list1.size()!=list2.size()){//两者间的电子运单中间表数据不相同
			if(list2!=null &&!list2.isEmpty()){
				shipOrderGoodsDao.delete(entity.getId());
			}
			for(ShipOrderGoods item:list1){//新增操作
				item.setId(shipOrderGoodsDao.getCustomerId());
				item.setShipOrderId(entity.getId());
				flag&=shipOrderGoodsDao.add(item);
			}
		}else {//两者间的电子运单中间表数据相同
			for(int i=0;i<list1.size();i++){
				ShipOrderGoods item=list2.get(i);
				Date tempCreateTime=item.getCreateTime();
				BeanUtils.copyProperties(list1.get(i), item);
				item.setShipOrderId(entity.getId());
				item.setUpdateTime(new Date());
				item.setCreateTime(tempCreateTime);
				flag&=shipOrderGoodsDao.update(item);
			}
		}
		flag&= super.update(tempShipOrder);
		return flag;
	}

}
